<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form_step\util\step;

use builder\base\Builder;
use builder\base\UndefinedParamsException;
use builder\base\ViewBuilderInterface;
use yii\base\InvalidArgumentException;

/**
 * 项内容
 * @author cleverstone
 * @since ym2.0
 */
class Item extends SiType
{
    /**
     * @var array
     */
    private $_options = [
        'title' => '',
        'content' => '',
        'callId' => '',
    ];

    /**
     * 步标题
     * @param string $title
     * @return $this
     */
    public function title($title)
    {
        $this->_options['title'] = (string)$title;
        return $this;
    }

    /**
     * 设置调用ID(用于JS表单数据获取; 即:挂载在JS对象[ym2PageDataExport]中的方法名称,内置表单构建器ID已自动挂载;)
     * 在步内容设置时,如果传入的[结果]是字符串,如需[下一步]校验和表单数据获取,则需要调用该方法,并传入挂载在JS对象[ym2PageDataExport]中的方法名称;
     * @param string $callId
     * @return $this
     */
    public function callId($callId)
    {
        $this->_options['callId'] = (string)$callId;
        return $this;
    }

    /**
     * 步骤内容
     * @param string|callable|Builder $content
     * @return $this
     * @throws UndefinedParamsException
     * @throws \ReflectionException
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\di\NotInstantiableException
     */
    public function content($content)
    {
        if (is_callable($content)) {
            $args = \Yii::$container->resolveCallableDependencies($content);
            if (empty($this->_options['callId'])) {
                foreach ($args as $arg) {
                    if ($arg instanceof Builder) {
                        //设置callId
                        $this->_options['callId'] = $arg->id;
                        break;
                    }
                }
            }

            $content = call_user_func_array($content, $args);
        }

        if (empty($this->_options['callId']) && $content instanceof Builder) {
            //设置callId
            $this->_options['callId'] = $content->id;
        }

        if ($content instanceof ViewBuilderInterface) {
            $content = $content->render(true);
        }

        if (!is_string($content)) {
            throw new InvalidArgumentException('Expected parameter [content] is string, But the others are given. ');
        }

        $this->_options['content'] = $content;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return $this->_options;
    }
}